﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Service.DataMaintenance;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 诊断结果维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class ZhenDuanJieGuoWeiHuController : Controller
    {
        private readonly IZhenDuanJieGuoWeiHu _IZhenDuanJieGuoWeiHu;
        private static readonly ILog log = LogManager.GetLogger(typeof(KeShiWeiHuController));
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICustomJWTService _ICustomJWTService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IZhenDuanJieGuoWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public ZhenDuanJieGuoWeiHuController(IZhenDuanJieGuoWeiHu IZhenDuanJieGuoWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IZhenDuanJieGuoWeiHu = IZhenDuanJieGuoWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }
        /// <summary>
        /// 诊断结果维护添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostJiBingAdd(JiBingAddClass JiBingAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZhenDuanJieGuoWeiHu.PostJiBingAdd(JiBingAddClass, YuanQuQianZhui));
        }
        /// <summary>
        /// 诊断结果维护修改
        /// </summary>
        /// <param name="JiBingUpdateClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutJiBingUpdate(JiBingUpdateClass JiBingUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.PutJiBingUpdate(JiBingUpdateClass));
        }
        /// <summary>
        /// 根据疾病名称查询
        /// </summary>
        /// <param name="strJiBingMingCheng"></param>
        /// <param name="strShiFouWanQuanPiPei"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetJiBingSel(string strJiBingMingCheng ,string strShiFouWanQuanPiPei,int intPage,int intPageSize)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IZhenDuanJieGuoWeiHu.GetJiBingSel(strJiBingMingCheng,strShiFouWanQuanPiPei, YuanQu,intPage,intPageSize));
        }
        /// <summary>
        /// 疾病通用名添加
        /// </summary>
        /// <param name="strTongYongMingMingCheng"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostTongYongMingAdd(JiBingTongYongMingAddClass JiBingTongYongMingAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IZhenDuanJieGuoWeiHu.PostTongYongMingAdd(JiBingTongYongMingAddClass, YuanQuQianZhui));
        }
        /// <summary>
        /// 疾病通用名修改
        /// </summary>
        /// <param name="strTongYongMingID"></param>
        /// <param name="strTongYongMing"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutTongYongMingUpdate(JiBingTongYongMingUpdateClass JiBingTongYongMingUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.PutTongYongMingUpdate(JiBingTongYongMingUpdateClass));
        }
        /// <summary>
        /// 疾病通用名删除
        /// </summary>
        /// <param name="strTongYongMingID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteTongYongMingDel(JiBingTongYongMingDelClass JiBingTongYongMingDelClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.DeleteTongYongMingDel(JiBingTongYongMingDelClass));
        }
        /// <summary>
        /// 根据通用名查询疾病通用名
        /// </summary>
        /// <param name="strTongYongMing"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTongYongMingSel(string strTongYongMing)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.GetTongYongMingSel(strTongYongMing));
        }
        /// <summary>
        /// 增加疾病对应通用名
        /// </summary>
        /// <param name="strTongYongMingID"></param>
        /// <param name="strJiBingID"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostJiBingDuiYingTongYongMing(JiBingDuiYingTongYongMingAddClass JiBingDuiYingTongYongMingAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.PostJiBingDuiYingTongYongMing(JiBingDuiYingTongYongMingAddClass));
        }
        /// <summary>
        /// 根据通用名ID查询通用名疾病
        /// </summary>
        /// <param name="strTongYongMingID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTongYongMingJiBingSel(string strTongYongMingID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.GetTongYongMingJiBingSel(strTongYongMingID));
        }
        /// <summary>
        /// 根据疾病ID删除疾病信息
        /// </summary>
        /// <param name="strJiBingID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteJiBingDel(JiBingDelClass JiBingDelClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.DeleteJiBingDel(JiBingDelClass));
        }
        /// <summary>
        /// 根据疾病ID查询疾病包含关键字
        /// </summary>
        /// <param name="strJiBingID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetSelJiBingBaoHanGuanJianZi(string strJiBingID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.GetSelJiBingBaoHanGuanJianZi(strJiBingID));
        }
        /// <summary>
        /// 疾病包含关键字添加
        /// </summary>
        /// <param name="strJiBingID"></param>
        /// <param name="strGuanJianZiMingCheng"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostJiBingBaoHanGuanJianZiAdd(JiBingBaoHanGuanJianZiAddClass JiBingBaoHanGuanJianZiAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.PostJiBingBaoHanGuanJianZiAdd(JiBingBaoHanGuanJianZiAddClass));
        }
        /// <summary>
        /// 疾病包含关键字修改
        /// </summary>
        /// <param name="strJiBingID"></param>
        /// <param name="strGuanJianZiMingCheng"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutJiBingBaoHanGuanJianZiUpdate(JiBingBaoHanGuanJianZiUpdateClass JiBingBaoHanGuanJianZiUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.PutJiBingBaoHanGuanJianZiUpdate(JiBingBaoHanGuanJianZiUpdateClass));
        }
        /// <summary>
        /// 疾病包含关键字删除
        /// </summary>
        /// <param name="JiBingBaoHanGuanJianZiDelClass"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DelJiBingBaoHanGuanJianZiDelete(JiBingBaoHanGuanJianZiDelClass JiBingBaoHanGuanJianZiDelClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.DelJiBingBaoHanGuanJianZiDelete(JiBingBaoHanGuanJianZiDelClass));
        }

        /// <summary>
        /// 疾病对应通用名删除
        /// </summary>
        /// <param name="JiBingDuiYingTongYongMingDelClass"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DelJiBingDuiYingTongYongMingDelete(JiBingDuiYingTongYongMingDelClass JiBingDuiYingTongYongMingDelClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IZhenDuanJieGuoWeiHu.DelJiBingDuiYingTongYongMingDelete(JiBingDuiYingTongYongMingDelClass));
        }

    }
}
